package b1.scenes.home.police
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.utils.BFont;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.text.*;

	public class CaseItem extends Sprite
	{
		private var battleCase:BattleCaseInfo;
		private	const animalPosY:int = 104;
		private var caseTip:Sprite;
		
		public function CaseItem(battleCase:BattleCaseInfo)
		{
			super();
			this.battleCase = battleCase;
			//背景框
			var TabItemBackground:Class = ResourceHelper.getInstance().getClassByName("TabItemBackground");
			var tabItemBackground:Sprite = new TabItemBackground(); 
			tabItemBackground.width = 125;
			tabItemBackground.height = 125;
			addChild(tabItemBackground);
			
			var clue:ClueInfo = this.battleCase.clues[this.battleCase.clues.length - 1];
			var clueConfig:ClueConfig = Config.clueConfigs[clue.clueIndex];

			//证物袋图片
			var ClueBagImg:Class = ResourceHelper.getInstance().getClassByName("ClueMcBackground");
			var clueBagImg:Sprite = new ClueBagImg();
			clueBagImg.width = 55;
			clueBagImg.scaleY = clueBagImg.scaleX;
			clueBagImg.rotation = -80;
			clueBagImg.x = 20;
			clueBagImg.y = 60;
			addChild(clueBagImg);
			
			//线索图片
			var imgStr:String;
			if (clue.clueIndex == 1)
			{
				imgStr = "Clue" + clue.clueIndex.toString() + "_" + 0;
			}
			else if(clue.clueIndex == 0)
			{
				imgStr = "Clue" + clue.clueIndex.toString() + "_0_" + clue.jawType.toString();
			}
			else
			{
				imgStr = "Clue" + clue.clueIndex.toString() + "_" + clue.clueValue.toString();
			}
			var ClueImage:Class = ResourceHelper.getInstance().getClassByName(imgStr);
			var clueImg:Sprite = new ClueImage();
			var clueImgDiag:Number = Math.sqrt(clueImg.width * clueImg.width + clueImg.height * clueImg.height);
			var ratio:Number = 60 / clueImgDiag;
			clueImg.scaleX = ratio;
			clueImg.scaleY = ratio;
			clueImg.x = 45;
			clueImg.y = 20;
			addChild(clueImg);
			
			//标签图片
			var ClueLabelImg:Class = ResourceHelper.getInstance().getClassByName("ClueLabelImg");
			var clueLabelImg:Sprite = new ClueLabelImg();
			clueLabelImg.width = 40;
			clueLabelImg.scaleY = clueLabelImg.scaleX;
			clueLabelImg.rotation = 50;
			clueLabelImg.x = 30;
			clueLabelImg.y = 32;
			addChild(clueLabelImg);
			
			//盖章
			if (battleCase.clues.length > 0)
			{
				var leftTime:int = (clue.timeToDisappear - ServerHelper.getInstance().now()) / 60;
				var StampImageStr:String;
				if (leftTime <= 0 || battleCase.ownerDetectedFlag && battleCase.otherDetectedFlag)
				{
					//破案
					if (battleCase.detectiveOwnerId > 0)
					{
						StampImageStr = "CaseBrokenStamp";
					}
					//结案
					else
					{
						StampImageStr = "EstablishCaseStamp";
					}
				}
				//调查中
				else
				{
					StampImageStr = "EstablishCaseStamp";
				}
				var StampImage:Class = ResourceHelper.getInstance().getClassByName(StampImageStr);
				var stamp:Sprite = new StampImage();
				stamp.width = 60;
				stamp.scaleY = stamp.scaleX;
				stamp.rotation = -10;
				stamp.x = tabItemBackground.width / 2 - 10;
				stamp.y = tabItemBackground.height / 2 - 10;
				addChild(stamp);
			}
		
			//标题名字
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			titleText.text = battleCase.getCaseName(); 
			titleText.x = (tabItemBackground.width - titleText.width) / 2;
			titleText.y = 75;
			addChild(titleText);
			
			var victimText:TextField = new TextField();
			victimText.selectable = false;
        	victimText.autoSize = TextFieldAutoSize.LEFT;
			victimText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000);
			victimText.text = "受害人: " + battleCase.loserOwnerName.substr(0,4);
			victimText.x = titleText.x;
			victimText.y = titleText.y + 20;
			addChild(victimText);
			
			addEventListener(MouseEvent.CLICK, onCaseItemClick);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onCaseItemClick(event:MouseEvent):void
		{
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
//			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(
				[1,0,0,0,15,
			     0,1,0,0,15,
			     0,0,1,0,15,
			     0,0,0,1,0 ])];
			caseTip = new Sprite();
			var roundRec:RoundRec = new RoundRec(130, 125, 0.8, BFont.roundRecColor);
			caseTip.addChild(roundRec);
			
			var gap:int = 10;
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
			titleText.x = gap;
			titleText.text = battleCase.getCaseName();
			titleText.y = 10;
			caseTip.addChild(titleText);
			//受害者
			var nameText:TextField = new TextField();
			nameText.selectable = false;
        	nameText.autoSize = TextFieldAutoSize.LEFT;
			nameText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x000000);
			nameText.x = gap;
			nameText.appendText("受害者: " + battleCase.loserOwnerName.substr(0.4));
			nameText.appendText("\n失窃骨头: " + battleCase.caseBones.toString());
			nameText.appendText("\n线索个数: " + battleCase.clues.length);
			if (battleCase.clues.length > 0)
			{
				var clue:ClueInfo = this.battleCase.clues[this.battleCase.clues.length - 1];
				var leftTime:int = clue.timeToDisappear - ServerHelper.getInstance().now();
				if (leftTime <= 0 || battleCase.ownerDetectedFlag && battleCase.otherDetectedFlag)
				{
					if (battleCase.detectiveOwnerId > 0)
					{
						nameText.appendText("\n破案侦探: " + battleCase.detectiveOwnerName);
					}
					else
					{
						nameText.appendText("\n已结案");
					}
				}
				else
				{
					var timeLeft:String = Clock.setTimeDisplayFormatToMinute(leftTime);
					nameText.appendText("\n离结案: " + timeLeft);
				}
			}
			nameText.y = 30;
			caseTip.addChild(nameText);
			
			if (this.x < 350)
			{
				caseTip.x = this.x + 280;
			}
			else
			{
				caseTip.x = this.x + 27;
			}
			caseTip.y = this.y + 105;
			SceneContainer.getInstance().currentScene.addChild(caseTip);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
//			Mouse.cursor = "auto";
			this.filters = null;
			if (null != caseTip && null != caseTip.parent)
			{
				caseTip.parent.removeChild(caseTip);
			}
		}
		
	}
}